from create_import import *
from table_list import *
import json
from datetime import datetime
import random, string


def yyyy_MM_dd():
    return datetime.now().strftime("%Y-%m-%d")


def add_public_params(h):
    h["dataTime"] = yyyy_MM_dd()
    h["dataUser"] = "dataUser"
    h["dataComment"] = "dataComment"


def random_en_string(size):
    return "".join(random.choice(string.ascii_letters) for _ in range(size))


def random_ch_string(length):
    base = "\u4e00"  # 汉字的起始Unicode码点
    end = "\u9fff"  # 汉字的结束Unicode码点
    chinese_chars = [chr(random.randint(ord(base), ord(end))) for _ in range(length)]
    return "".join(chinese_chars)


def random_test_data(dbtype: str, english=False):
    dbtype = dbtype.strip()
    if "varchar" in dbtype:
        n = dbtype[dbtype.find("(") + 1 : -1 :]
        if english:
            return random_en_string(int(n))
        return random_ch_string(int(n) // 3) + random_en_string(int(n) % 3)
    if "smallint" in dbtype:
        return random.randint(-32768, 32767)
    if "int" in dbtype:
        return random.randint(-2147483648, 2147483647)


def create_test_url_json(table: str):
    with open(f"{local_dir}/{table}.txt", "r", encoding="utf-8") as f:
        outputpath = f"{local_dir}/{table}"
        if not os.path.exists(outputpath):
            os.makedirs(outputpath)
        outputjson = f"{outputpath}/{table}_json_save.txt"
        ot = output(outputjson)
        lines = f.readlines()
        names = []
        for line in lines:
            names.append(line.rstrip().lower().split(","))

        cols = lines[1].rstrip().split(",")
        # comments = lines[0].rstrip().split(",")
        types = lines[2].rstrip().split(",")
        json_save = dict()
        for k in range(len(cols)):
            col = cols[k]
            # comment = comments[k]
            word = col.strip()
            tpye = types[k]
            w = uppercase_except_first(word)
            json_save[w] = random_test_data(tpye, english=True)
        add_public_params(json_save)
        print(json.dumps(json_save, ensure_ascii=False), file=ot)


if __name__ == "__main__":
    # for table in table_list:
    #     create_test_url_json(table)
    # print(50 / 3, random_ch_string(int(50 / 3)))

    a = []
    a.append("22134b8c600b4f19d0471c908a29cd25")
    a.append("88e3d9c2ac20c10032ee9817bd68be8a")
    print(json.dumps(a))
